<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="自习室" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zixishiId">
              <j-dict-select-tag :disabled="canEdit" type="list" v-model="model.zixishiId" dictCode="sr_zixishi,name,id" placeholder="请选择自习室" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="区域" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="quyuId">
              <j-dict-select-tag :disabled="canEdit" ref="quyudict" type="list" v-model="model.quyuId" :dictCode="quyuDictCode" placeholder="请选择区域" @change="quyuChange"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input :disabled="canEdit" v-model="model.code" placeholder="请输入编码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="list" v-model="model.status" dictCode="seat_status" placeholder="请选择状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="devices">
              <j-multi-select-tag type="select" v-model="model.devices" dictCode="sr_device,device_name,uid" placeholder="请关联设备" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pic">
              <j-image-upload isMultiple :number=1 v-model="model.pic" ></j-image-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'SeatForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        canEdit: true,
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/zixishi/seat/add",
          edit: "/zixishi/seat/edit",
          queryById: "/zixishi/seat/queryById"
        },
        quyuDictCode: "sr_quyu,name,id,zixishi_id=1"
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      
    },
    watch:{
      'model.zixishiId': function (newVal,oldVal) {
        console.log(newVal + ',' + oldVal);
        if(oldVal){
          this.model.quyuId = '';
        }
        this.quyuDictCode = "sr_quyu,name,id,zixishi_id = '"+newVal+"'";
      }
    },
    methods: {
      quyuChange(val){
        // this.model.quyuId = val;
        this.$forceUpdate();
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.$nextTick(res => {

        })
        this.visible = true;
        if(this.model.id){
          this.canEdit = true;
        }else{
          this.canEdit = false;
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>